﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using Relocation.Com.Tools;
using Relocation.Data;
using Relocation.Com;
using Relocation.Base;


namespace Relocation.UI
{
	public partial class PlacementList : PlacementListHelp
	{
		private Relocatee _Relocatee;
		public Relocatee Relocatee
		{
			get
			{
				return this._Relocatee;
			}
			set
			{
				this._Relocatee = value;				
				this.ObjectQuery = this._Relocatee.Placements.CreateSourceQuery();
				this.people = _Relocatee.persons.HasValue ? (int)_Relocatee.persons : 0;
			}
		}

		public Placement SelectedPlacement;
		int people;//relocatees中的安置人数

		public PlacementList()
			: base()
		{
			InitializeComponent();
			Initialize();
		}

		public PlacementList(Session session, Relocatee relocatee)
			: base(session)
		{
			InitializeComponent();
			this.Relocatee = relocatee;
			Initialize();
			this.Refresh();

		}

		public PlacementList(Session session, Relocatee relocatee, Placement placement)
			: this(session, relocatee)
		{
			this.SelectedPlacement = placement;
		}	

		private void Initialize()
		{
			ControlTag tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
			this.ButtonAdd.Tag = tag;
			this.ButtonEdit.Tag = tag;
			this.ButtonDel.Tag = tag;
			this.S_name.Tag = new ControlTag(null, null, new SearchInfo(Placement.GetPropName(t => t.name)));
			this.S_idNumber.Tag = new ControlTag(null, null, new SearchInfo(Placement.GetPropName(t=>t.id_number)));
			this.DataSourceRander = new DataSourceRanderHandel(sortRelation);
		}

		/// <summary>
		/// 调用父窗体的refresh
		/// </summary>		
		private void BaseRefresh()
		{
			RelocateesWindow relocateesWindow = this.ParentForm as RelocateesWindow;
			if (relocateesWindow != null)
				relocateesWindow.Refresh();
		}

		/// <summary>
		/// 按照家庭关系排序
		/// </summary>
		private List<Placement> sortRelation(List<Placement> placementList)
		{
			Placement p1 = placementList.FirstOrDefault(t => t.relation.Equals("配偶"));
			if (p1 != null)
			{
				placementList.Remove(p1);
				placementList.Insert(0, p1);
			}

			Placement p2 = placementList.FirstOrDefault(t => t.relation.Equals("户主"));
			if (p2 != null)
			{
				placementList.Remove(p2);
				placementList.Insert(0, p2);
			}
			return placementList;
		}

		/// <summary>
		/// 添加
		/// </summary>
		public void ButtonAdd_Click(object sender, EventArgs e)
		{
			try
			{
				using (PlacementWindow placmentWindow = new PlacementWindow(this.Session, this.Relocatee))
				{
					if (placmentWindow.ShowDialog().Equals(DialogResult.OK))
					{
						this.Reload();
						this.BaseRefresh();
					}
				}

			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("保存失败！");
			}
		}

		/// <summary>
		/// 修改
		/// </summary>
		public void ButtonEdit_Click(object sender, EventArgs e)
		{
			try
			{
				Placement placement = this.GetSelectEntity();
				if (placement == null)
					return;
				using (PlacementWindow placementWindow = new PlacementWindow(this.Session, placement))
				{
					if (placementWindow.ShowDialog(this).Equals(DialogResult.OK))
						this.BaseRefresh();
				}				
				
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MyMessagebox.Error("保存失败！");
			}

		}


		/// <summary>
		/// 删除
		/// </summary>
		public void ButtonDel_Click(object sender, EventArgs e)
		{
			try
			{
				Placement placement = this.GetSelectEntity();
				if (placement == null)
					return;
				if (placement.relation.Equals("户主"))
				{
					MyMessagebox.Show("户主信息不可删除！");
					return;
				}
				TreeNode node = this.Session.GetMainTreeView().FindNode(placement);

				this.DeleteEntity(placement);
				this.Relocatee.persons = this.Relocatee.Placements.Count;
				this.Session.DataModel.SaveChanges();
				if (node != null)
					node.Remove();
				this.BaseRefresh();
			} catch (Exception ex)
			{
				MyMessagebox.Error("删除失败！");
				Log.Error(ex.GetInnerExceptionMessage());
			}
			this.Reload();
		}

	

		private void PlacementList_Load(object sender, EventArgs e)
		{
			try
			{
				SelectEntity(this.SelectedPlacement);
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
			}
		}

		public override DataGridView GetDataGridView()
		{
			return this.dataGridView;
		}






		//internal void SelectEntity(Placement relocate)
		//{
		//        if (relocate != null)
		//        {
		//                this.dataGridView.ClearSelection();
		//                Placement tempPlacement;
		//                foreach (DataGridViewRow row in dataGridView.Rows)
		//                {
		//                        tempPlacement = row.DataBoundItem as Placement;
		//                        if (tempPlacement == null)
		//                                continue;
		//                        if (tempPlacement.placement_id.Equals(relocate.placement_id))
		//                        {
		//                                row.Selected = true;
		//                                //this.dataGridView.CurrentCell = row.Cells[1];
		//                                break;
		//                        }
		//                }
		//        }
		//}
	}
}